package com.sinba.common.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.system.dto.LotteryDTO;
import com.sinba.common.system.service.IGenerateRegisterCodeService;
import com.sinba.common.user.dto.RegisterCodeDTO;

@SuppressWarnings("serial")
public class GenerateRegisterCodeAction extends ActionSupport{
	private int num;
	private String remark;
	private String code;
	private List<RegisterCodeDTO> dtos = new ArrayList<RegisterCodeDTO>();
	private Boolean flag = false;
    private PageDTO pageDTO;
	private RegisterCodeDTO dto = new RegisterCodeDTO();
    private Long[] ids;
    private int page = 1;
    private int rows = 10;
    private String sidx;
    private String sord;
	@Autowired
	private IGenerateRegisterCodeService generateRegisterCodeService;
	
	public String generateRegisterCode(){
		dtos = generateRegisterCodeService.generateCode(num,remark);
		return "resigterCodes";
	}
	
	public String activationCodeVerification(String code){
		flag = generateRegisterCodeService.registerCodeVerification(code);
		return "flag";
	}
	
	public String findRegisterCode(){
        int start = ( page - 1 ) * rows;
		pageDTO = generateRegisterCodeService.searchCode(dto, start, rows, sidx, sord);
		pageDTO.setPage( page );
		pageDTO.setRows( rows );
		return "pageDTO";
	}
	
	public String lotteryRegisterCode(){
		LotteryDTO lotteryDTO = getLotteryResult();
		if(lotteryDTO.getWinning()){
			dto = generateRegisterCodeService.getToken();
			dto.setMsg(lotteryDTO.getMsg()+dto.getToken());
		}else{
			dto.setMsg(lotteryDTO.getMsg());
		}
		dto.setAngle(lotteryDTO.getAngle());
		return "resigterCode";
	}
	
	public void deleRegisterCode(){
		generateRegisterCodeService.deleRegisterCode(code);
	}

	public void deleRegisterCodes(){
		generateRegisterCodeService.deleRegisterCodes(ids);
	}
	

	public void banRegisterCode() {
		generateRegisterCodeService.banRegisterCode(ids);
	}
	
	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public PageDTO getPageDTO() {
		return pageDTO;
	}

	public void setPageDTO(PageDTO pageDTO) {
		this.pageDTO = pageDTO;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public RegisterCodeDTO getDto() {
		return dto;
	}


	public void setDto(RegisterCodeDTO dto) {
		this.dto = dto;
	}


	public List<RegisterCodeDTO> getDtos() {
		return dtos;
	}


	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public void setDtos(List<RegisterCodeDTO> dtos) {
		this.dtos = dtos;
	}


	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	
	/**
	 * 获取随机生成的角度
	 * @return
	 */
	private LotteryDTO getLotteryResult(){
		List<LotteryDTO> lotterys = new ArrayList<LotteryDTO>();
		LotteryDTO lottery1 = new LotteryDTO();
		lottery1.setAngle("14");
		lottery1.setMsg("恭喜！获取凭证：");
		lottery1.setWinning(true);
		lotterys.add(lottery1);

		LotteryDTO lottery2 = new LotteryDTO();
		lottery2.setAngle("364");
		lottery2.setMsg("恭喜！获取凭证：");
		lottery2.setWinning(true);
		lotterys.add(lottery2);
		
		LotteryDTO lottery3 = new LotteryDTO();
		lottery3.setAngle("44");
		lottery3.setMsg("不要灰心");
		lotterys.add(lottery3);
		
		LotteryDTO lottery4 = new LotteryDTO();
		lottery4.setAngle("74");
		lottery4.setMsg("神马也没有");
		lotterys.add(lottery4);
		
		LotteryDTO lottery5 = new LotteryDTO();
		lottery5.setAngle("104");
		lottery5.setMsg("祝您好运");
		lotterys.add(lottery5);
		
		LotteryDTO lottery6 = new LotteryDTO();
		lottery6.setAngle("134");
		lottery6.setMsg("恭喜！获取凭证：");
		lottery6.setWinning(true);
		lotterys.add(lottery6);
		
		LotteryDTO lottery7 = new LotteryDTO();
		lottery7.setAngle("164");
		lottery7.setMsg("再接再厉");
		lotterys.add(lottery7);
		
		LotteryDTO lottery8 = new LotteryDTO();
		lottery8.setAngle("194");
		lottery8.setMsg("神马也没有");
		lotterys.add(lottery8);
		
		LotteryDTO lottery9 = new LotteryDTO();
		lottery9.setAngle("224");
		lottery9.setMsg("运气先攒着");
		lotterys.add(lottery9);
		
		LotteryDTO lottery10 = new LotteryDTO();
		lottery10.setAngle("254");
		lottery10.setMsg("恭喜！获取凭证：");
		lottery10.setWinning(true);
		lotterys.add(lottery10);
		
		LotteryDTO lottery11 = new LotteryDTO();
		lottery11.setAngle("284");
		lottery11.setMsg("要加油哦");
		lotterys.add(lottery11);
		
		LotteryDTO lottery12 = new LotteryDTO();
		lottery12.setAngle("314");
		lottery12.setMsg("神马也没有");
		lotterys.add(lottery12);
		
		LotteryDTO lottery13 = new LotteryDTO();
		lottery13.setAngle("344");
		lottery13.setMsg("谢谢参与");
		lotterys.add(lottery13);

		return lotterys.get(new Random().nextInt(lotterys.size()));
	}
}
